package org.ainlolcat.ainscience.UI;

import org.ainlolcat.ainscience.Controller;
import org.ainlolcat.ainscience.conflictResolver.Conflict;
import org.ainlolcat.ainscience.conflictResolver.ConflictListener;
import org.ainlolcat.ainscience.tools;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: ain
 * Date: 10/29/11
 * Time: 10:28 PM
 * To change this template use File | Settings | File Templates.
 */
public class ConflictResolver {
    public static int LIST_TYPE = 1;
    public static int STRING_TYPE = 2;
    public static int INT_TYPE = 3;
    public static int FILE_TYPE = 4;
    public static int SELECT_LIST_TYPE = 5;

    JFrame frame;
    JTextField newname;
    JLabel message;
    JList list ;
    JCheckBox useRelative;

    Conflict conflict;
    ConflictListener listener;

    public void resolve(Conflict conflict, ConflictListener listener){
        this.conflict = conflict;
        this.listener = listener;
        frame = new JFrame("Conflict Resolver");
        frame.setSize(600,300);
        frame.setLocation(tools.getScreenSize().width/2-300,
                tools.getScreenSize().height/2-150);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(null);
        if (conflict.getType()==STRING_TYPE){
            handleStringType();
        } else if (conflict.getType()==LIST_TYPE){
            handleListType();
        } else if (conflict.getType()==SELECT_LIST_TYPE){
            handleSelectType();
        } else if (conflict.getType()==FILE_TYPE){
            handleFileType();
        } else {
            JOptionPane.showMessageDialog(frame,"Conflict type "+conflict.getType()+" unsupported");
        }
    }

    public void handleStringType(){
        message =  new JLabel(conflict.getMessage());
        message.setLocation(10, 0);
        message.setSize(600, 50);
        frame.add(message);
        newname = new JTextField((String) conflict.getOldValue());
        newname.setLocation(10,50);
        newname.setSize(300, 50);
        frame.add(newname);
        JButton confirm = new JButton("OK");
        confirm.setLocation(370,50);
        confirm.setSize(100, 50);
        confirm.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (conflict.tryResolve(newname.getText())){
                    listener.resolved(conflict);
                    frame.dispose();
                }
                else{
                    message.setText(conflict.getMessage());
                    JOptionPane.showMessageDialog(frame,conflict.getMessage());
                }
            }
        });
        frame.add(confirm);
        frame.setVisible(true);
        JOptionPane.showMessageDialog(frame, conflict.getMessage());
    }
    public void handleListType(){
        message =  new JLabel(conflict.getMessage());
        message.setLocation(100, 0);
        message.setSize(500, 50);
        frame.add(message);
        newname = new JTextField();
        newname.setLocation(100,50);
        newname.setSize(300, 50);
        frame.add(newname);
        Vector vector = new Vector((List)conflict.getOldValue());
        JList list = new JList(vector);
        list.setSize(80,280);
        list.setLocation(10, 10);
        frame.add(list);
        JButton confirm = new JButton("OK");
        confirm.setLocation(470,50);
        confirm.setSize(100, 50);
        confirm.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (conflict.tryResolve(newname.getText())){
                    listener.resolved(conflict);
                    frame.dispose();
                }
                else{
                    message.setText(conflict.getMessage());
                    JOptionPane.showMessageDialog(frame,conflict.getMessage());
                }
            }
        });
        frame.add(confirm);
        frame.setVisible(true);
        JOptionPane.showMessageDialog(frame,conflict.getMessage());
    }
    public void handleSelectType(){
        Vector vector = new Vector((List)conflict.getOldValue());
        list= new JList(vector);
        list.setSize(80, 280);
        list.setLocation(10, 10);
        list.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (conflict.tryResolve((String)list.getSelectedValue())){
                    listener.resolved(conflict);
                    frame.dispose();
                }
                else{
                    message.setText(conflict.getMessage());
                    JOptionPane.showMessageDialog(frame,conflict.getMessage());
                }
            }
        });
        frame.add(list);
        frame.setVisible(true);
        JOptionPane.showMessageDialog(frame,conflict.getMessage());
    }
    public void handleFileType(){
        message =  new JLabel(conflict.getMessage());
        message.setLocation(10, 0);
        message.setSize(600, 50);
        frame.add(message);
        useRelative = new JCheckBox("use relative path");
        useRelative.setSelected(true);
        useRelative.setSize(100, 50);
        useRelative.setLocation(10, 60);
        frame.add(useRelative);
        JButton confirm = new JButton("Open");
        confirm.setLocation(370,50);
        confirm.setSize(100, 50);
        confirm.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JFileChooser fc = new JFileChooser();
                fc.showOpenDialog(null);
                File file = fc.getSelectedFile();
                String filename = null;
                try {
                    filename = file.getCanonicalPath();
                } catch (IOException e1) {
                    JOptionPane.showMessageDialog(frame, "CR: Error occured: "+e1.getLocalizedMessage());
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                if (useRelative.isSelected()){
                    String home = null;
                    try{
                        home = Controller.getInstance().getProject().getHomeDir();
                        System.out.println("CR: home is: "+home);
                    }catch(NullPointerException nex){
                        nex.printStackTrace();
                    }
                    if (home!=null && filename.startsWith(home)){
                        System.out.println("CR: replace");
                        filename = filename.replaceFirst(home,"%HOME%");
                    }
                }
                if (conflict.tryResolve(filename)){
                    listener.resolved(conflict);
                    frame.dispose();
                }
                else{
                    message.setText(conflict.getMessage());
                    JOptionPane.showMessageDialog(frame,conflict.getMessage());
                }
            }
        });
        frame.add(confirm);
        frame.setVisible(true);
        JOptionPane.showMessageDialog(frame, conflict.getMessage());
    }

}
